import {checkMusic, getMusicComments, getMusicLyric, getSongDetail} from "../api/api";
import AudioManger from "../common/AudioManger";
import {Notify} from "vant";
import Loading from "../common/Loading";

export default {
    namespaced: true,
    state: {
        musicId: 0,
        musicUrl: 0,
        musicData: null,
        musicCurrentTime: 0,
        musicMaxTime: 0,
        musicIsPlay: false,
        musicLoading: false,
        musicLyricData: null,
        // 歌曲评论
        musicCommentData: [],
        musicCommentPage: 1,
        // playlist
        musicPlayListIds: []
    },
    mutations: {
        setMusicId(state,id){
            state.musicId = id;
        },
        setMusicUrl(state,url){
            state.musicUrl = url;
        },
        setMusicData(state,data){
            state.musicData = data;
        },
        setMusicCurrentTime(state,time){
            state.musicCurrentTime = time;
            // audio.currentTime = time;
        },
        setMusicMaxTime(state,time){
            state.musicMaxTime = time;
        },
        setMusicIsPlay(state,isPlay){
            state.musicIsPlay = isPlay;
        },
        setMusicLoading(state,isLoading){
            state.musicLoading = isLoading;
        },
        setMusicLyricData(state,lyric){
            state.musicLyricData = lyric;
        },
        setMusicCommentData(state,commentsData){
            state.musicCommentData = commentsData;
        },
        setMusicCommentPage(state,page){
            state.musicCommentPage = page;
        },
        addMusicPlayListsData(state,...data){
            state.musicPlayListIds.push(...data);
        },
        removeMusicPlayListsData(state,index){
            state.musicPlayListIds.splice(index,1);
        },
        resetMusicPlayListsData(state){
            state.musicPlayListIds = [];
        }
    },
    actions: {
        play(){
            return AudioManger.play();
        },
        pause(){
            AudioManger.pause();
        },
        playToId(store,id){
            let currentMusicId = store.state.musicId;
            let playlistsIds = store.state.musicPlayListIds;
            if (currentMusicId === id){
                AudioManger.setCurrentTime(0);
                return;
            }
            store.commit("setMusicLoading",true);
            checkMusic(id).then((res)=>{
                if(res.success){
                    console.log(res.success)
                    getSongDetail(id).then((res)=>{
                        let songData = res.songs[0] ?? null;
                        let audioSrc = "https://music.163.com/song/media/outer/url?id="+id+".mp3";
                        AudioManger.setSrc(audioSrc);
                        store.commit("setMusicUrl",audioSrc);
                        store.commit("setMusicData",songData);
                        store.commit("setMusicId",id);
                        store.commit("setMusicMaxTime",songData.dt);
                        store.commit("setMusicCurrentTime",0);
                        store.dispatch("play");
                        store.commit("setMusicLoading",false);
                        if(playlistsIds.indexOf(id) === -1){
                            store.commit("addMusicPlayListsData",id);
                        }

                    }).catch(()=>{
                        store.commit("setMusicLoading",false);
                        Notify("获取歌曲信息失败");
                    });
                    getMusicLyric(id).then((res)=>{
                        store.commit("setMusicLyricData",res);
                    }).catch(()=>{
                        store.commit("setMusicLyricData",null);
                    });
                }else{
                    store.commit("setMusicLoading",false);
                    Notify(res.message);
                }
            }).catch((res)=>{
                store.commit("setMusicLoading",false);
                Notify("暂无版权，无法播放!");
            });
        },

        next(store){
            let musicPlayListIds = store.state.musicPlayListIds;
            let musicId = store.state.musicId;
            let index = musicPlayListIds.indexOf(musicId);
            let nextId = musicPlayListIds[index + 1];
            if(index === -1 || !nextId){
                if(musicPlayListIds.length > 0){
                    store.dispatch("playToId",musicPlayListIds[0]);
                    return true;
                }
                return false;
            }
            if(nextId){
                store.dispatch("playToId",nextId);
                return true;
            }
        },
        last(store){
            let musicPlayListIds = store.state.musicPlayListIds;
            let musicId = store.state.musicId;
            let index = musicPlayListIds.indexOf(musicId);
            let nextId = musicPlayListIds[index - 1];
            if(index === -1 || !nextId){
                if(musicPlayListIds.length > 0){
                    store.dispatch("playToId",musicPlayListIds[0]);
                    return true;
                }
                return false;
            }
            if(nextId){
                store.dispatch("playToId",nextId);
                return true;
            }
        },
        getCommentData(store){
            let state = store.state;
            let commentData = state.musicCommentData;
            console.log(state.musicId);
            Loading.show();
            return getMusicComments(state.musicId,state.musicCommentPage).then((res)=>{
                Loading.hide();
                let comments = res.comments;
                if(commentData){
                    commentData.comments.push(...comments);
                    store.commit("setMusicCommentData",commentData);
                }else{
                    store.commit("setMusicCommentData",res);
                }
            }).catch(()=>{
                Loading.hide();
            });
        },
        nextCommentData(store){
            let state = store.state;
            let commentData = state.musicCommentData;
            if(commentData){
                store.commit("setMusicCommentPage",(state.musicCommentPage+1));
                return store.dispatch("getCommentData");
            }else{
                store.commit("setMusicCommentPage",1);
                return store.dispatch("getCommentData");
            }
        },
        resetCommentData(store) {
            store.commit("setMusicCommentPage",1);
            store.commit("setMusicCommentData",null);
        }
    }
}